import os
from os.path import dirname,abspath
import numpy as np
import pandas as pd


from lib.metrics.mae_metrics import mae_score_metrics
from lib.metrics.pred_metrics import predictive_score_metrics
from lib.metrics.dis_metrics import discriminative_score_metrics
from lib.metrics.visualization_metrics import visualization
from lib.data import water_data_loading, Dataset
from utils import get_logger


def metric(ori_data, impute_data, logger):
    # [n_sample, seq_len, feature_dim]
    # log_dir = os.path.join('output', 'impute', log_dir)
    # logger = get_logger(log_dir, log_dir, 'impute.log')
    metric_results = dict()
    metric_results['mae'] = mae_score_metrics(ori_data, impute_data)

    discriminative_score = list()
    for _ in range(10):
        temp_disc = discriminative_score_metrics(ori_data, impute_data)
        discriminative_score.append(temp_disc)
    discriminative_score = np.sort(discriminative_score)
    logger.info(discriminative_score)
    metric_results['discriminative'] = np.mean(discriminative_score[2:-2])

    # 2. Predictive score
    # predictive_score = list()
    # for tt in range(10):
    #     plot_pred = True if tt == 0 else False
    #     temp_pred = predictive_score_metrics(ori_data, impute_data, None, log_dir, plot_pred)
    #     predictive_score.append(temp_pred)
    # predictive_score = np.sort(predictive_score)
    # metric_results['predictive'] = np.mean(predictive_score[2:-2])
    metric_results['predictive'] = 0.0
    # 3. Visualization (PCA and tSNE)
    # visualization(ori_data, impute_data, 'pca', log_dir)
    # visualization(ori_data, impute_data, 'tsne', log_dir)

    ## Print discriminative and predictive scores
    # msg = 'discriminative:{:.4f},predictive:{:.4f},mae:{:.4f}'\
    #     .format(metric_results['discriminative'], metric_results['predictive'], metric_results['mae'])
    # logger.info(msg)
    # print(metric_results)
    return metric_results

def load(data_path):
    seq_len=24
    data_path = os.path.join(dirname(dirname(dirname(abspath(__file__)))), data_path)
    dataset = Dataset('water', 'ori', seq_len, 'WATER_TEMPERATURE,PH_VALUE,TOTAL_NITROGEN,DISSOLVED_OXYGEN', '')
    ori_data = dataset.load_ori_data()
    imputed_data = dataset.load_imputed_data(data_path, seq_len)
    return ori_data[:len(imputed_data)], imputed_data

# 对插值结果进行重新评价，省的再插值一边然后才能评价
def load_metric():
    data_path = 'output_9_1/water/all/gru/fc_True_ln_True_res_True/z_dim/2_24_50000_0.001_0.9_24_128/masked_ratio-0.3/all_20000_0.0/imputed_data.csv'
    log_dir = os.path.join('output', 'impute')
    logger = get_logger(log_dir, log_dir, 'impute.log')
    logger.info(data_path)

    ori_data, imputed_data = load(data_path)
    metric_results = metric(ori_data, imputed_data, '.')
    msg = 'discriminative:{:.4f},predictive:{:.4f},mae:{:.4f}'\
        .format(metric_results['discriminative'], metric_results['predictive'], metric_results['mae'])

    logger.info(msg)

if __name__ == '__main__':
    load_metric()

    # WATER_TEMPERATURE,PH_VALUE,TOTAL_NITROGEN,DISSOLVED_OXYGEN
    #
    # indicators = 'WATER_TEMPERATURE,PH_VALUE,TOTAL_NITROGEN,DISSOLVED_OXYGEN'
    # mask_indicator = 'DISSOLVED_OXYGEN'
    # indicators = 'WATER_TEMPERATURE,AMMONIA,DISSOLVED_OXYGEN,TOTAL_NITROGEN'
    # mask_indicator = ''
    # seq_len = 24
    # shuffle = False
    # ori_data,_,_ = water_data_loading(seq_len, indicators, 'water_data.csv', shuffle)
    # last_impute_data,_,_ = water_data_loading(seq_len, indicators, 'last_interpolate_water_'+mask_indicator+'_data.csv', shuffle)
    #
    # metric(ori_data, last_impute_data, 'last')

